import Sockets from '../services/socket';

export default {
  namespace: 'ready',
  state: {
    data: {
    },
  },
  reducers: {
    init(state, { payload: data }) {
      return { ...state, data };
    },
    sys(state, { payload: data }) {
      state.data = data.outroom;
      const msg = data.msg;
      return { ...state, msg };
    },
  },
  effects: {
    establish({ payload: { roomNum, userName } }) {
      const id = Sockets.id;
      Sockets.emit('establish', { roomNum, userName, id });
    },
  },
  subscriptions: {
    watchSys({ dispatch }) {
      Sockets.on('sys', (data) => {
        dispatch({ type: 'sys', payload: data });
      });
    },
    setup({ dispatch }) {  // eslint-disable-line
      Sockets.on('goback', (data) => {
        dispatch({ type: 'init', payload: data });
      });
    },
  },
};
